//
//  DownloadProgressViewController.m
//  MapleLeaf
//
//  Created by WangZe on 15/11/30.
//  Copyright © 2015年 AntennaBabyTeam. All rights reserved.
//

#import "DownloadProgressViewController.h"
#import "DownLoadProgressTableViewCell.h"
#import <AFNetworking.h>
#import <SSZipArchive.h>
#import "WZLReadViewController.h"

@interface DownloadProgressViewController ()<UITableViewDataSource, UITableViewDelegate,NSURLSessionDelegate>


@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)NSMutableArray *downloadArr;
@end

@implementation DownloadProgressViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 50;
    
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.view addSubview:self.tableView];
        [self.tableView registerClass:[DownLoadProgressTableViewCell class] forCellReuseIdentifier:@"reuse"];
        self.tableView.rowHeight = 140;
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DownLoadProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.nameStr = self.nameStr;
    cell.dic = self.dataArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataArr[indexPath.row];
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Cartoon/%@",self.nameStr]];
    NSFileManager *manager = [NSFileManager defaultManager];
    
    // 判断是否存在文件夹
    if (![manager fileExistsAtPath:docPath]) {
        [manager createDirectoryAtPath:docPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    
    NSString *downloadPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@.zip", dic[@"name"],dic[@"num"]]];
    NSString *destinationPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@", dic[@"name"],dic[@"num"]]];
    if ([manager fileExistsAtPath:downloadPath]) {
        if (![manager fileExistsAtPath:destinationPath]) {
            
            [self readZip:downloadPath destinationPath:destinationPath];
        }
        else
        {
            NSLog(@"已经解压过");
        }
    }
    
    NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:destinationPath];
    NSLog(@"%@", direnum);
    NSMutableArray *arr = [NSMutableArray array];
    NSString *filename;
    while (filename = [direnum nextObject]) {
        if ([[filename pathExtension] isEqualToString:@"view"]) {
            [arr addObject:filename];
        }
    }
    NSLog(@"%@", arr);
    
    WZLReadViewController *VC = [[WZLReadViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    NSString *tempStr = dic[@"download"];
    NSString *str = [tempStr substringToIndex:tempStr.length - 4];
 
    VC.zipStr = str;
    VC.zipArr = arr;
}
- (void)readZip:(NSString *)downloadPath destinationPath:(NSString *)destinationPath
{
    NSString *zipPath = downloadPath;
    NSString *dPath = destinationPath;
    [SSZipArchive unzipFileAtPath:zipPath toDestination:dPath];
}
- (void)hehe
{
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
